package com.mmy.controller.ship;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.ship.TShipOrg;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.ship.impl.TShipOrgServiceImpl;

/**
 * 类的介绍：船舶公司信息
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class ShipOrgController {
	@Autowired
	TShipOrgServiceImpl tShipOrgServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/shiporg")
	public String ToPlaneOrg(Model model) {
		try {

			List<TShipOrg> lstShipOrg = tShipOrgServiceImpl.findAll();
			model.addAttribute("slist", lstShipOrg);

			return "webbackground/ship/shiporg";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createshiporg")
	public String ToCreateShipOrg() {

		return "webbackground/ship/createshiporg";
	}

	/**
	 * 函数功能说明： 新建船舶公司信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/subcreateshiporg")
	public String CreateShipOrg(TShipOrg tShipOrg, HttpSession session) throws Exception {
//		获取最大值
		Object oOrgID = tShipOrgServiceImpl.getMaxOrgID();
//		 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 
		if (oOrgID == null) {
			tShipOrg.setOrgID(1);
		} else {
			tShipOrg.setOrgID((int) oOrgID + 1);
		}
		String strOrgName = tShipOrg.getOrgName();

		try {

//			 判断名称是否为空 
			if (strOrgName == "" || strOrgName == null) {
				return "test";
			} else {
//				 判断名称是否已存在 
				if (tShipOrgServiceImpl.findByOrgName(strOrgName).size() > 0) {

					return "test";
				} else {
					tShipOrg.setCreateDate(new Date());
					TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
					tShipOrg.setCreator(tSysUser.getUserName());
					tShipOrgServiceImpl.save(tShipOrg.getOrgID());
					tShipOrgServiceImpl.save(tShipOrg);
					return "redirect:shiporg";

				}
			}

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除船舶公司信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/delshiporg")
	@ResponseBody
	public String DelShipOrg(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstOrgID = new ArrayList<>();

		for (String str : checklist) {
			lstOrgID.add(Integer.parseInt(str));
		}

		tShipOrgServiceImpl.delShipOrg(lstOrgID);

		return "webbackground/ship/shiporg";
	}

	/**
	 * 函数功能说明： 修改船舶公司信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateshiporg")
	public String EnterUpdateShipOrg(HttpServletRequest request, Model model) {

		int iOrgID = Integer.parseInt(request.getParameter("iOrgID"));

		TShipOrg tShipOrg = tShipOrgServiceImpl.findByOrgID(iOrgID);
		model.addAttribute("tShipOrg", tShipOrg);
		return "webbackground/ship/updateshiporg";

	}

	@RequestMapping(value = "/updateshiporg")
	public String UpdateShipOrg(TShipOrg tShipOrg, HttpSession session) throws Exception {
		try {

//			 定义一个新的实体类对象 
			TShipOrg dtShipOrg = tShipOrgServiceImpl.findByOrgID(tShipOrg.getOrgID());
			dtShipOrg.setOrgName(tShipOrg.getOrgName());
			dtShipOrg.setName(tShipOrg.getName());
			dtShipOrg.setTel(tShipOrg.getTel());
			dtShipOrg.setMemo(tShipOrg.getMemo());
			dtShipOrg.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtShipOrg.setReviser(tSysUser.getUserName());
			tShipOrgServiceImpl.save(dtShipOrg);
			return "redirect:shiporg";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询船舶公司信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchshiporg")
	public String FindShipOrg(HttpServletRequest request, Model model) {

		String strOrgName = request.getParameter("OrgName");
		if (strOrgName == "") {
			List<TShipOrg> lstShipOrg = tShipOrgServiceImpl.findAll();
			model.addAttribute("slist", lstShipOrg);
			request.setAttribute("strOrgName", strOrgName);
			return "webbackground/ship/shiporg";
		} else {
			List<TShipOrg> lstShipOrg = tShipOrgServiceImpl.findByOrgName(strOrgName);
			model.addAttribute("slist", lstShipOrg);
			request.setAttribute("strOrgName", strOrgName);
			return "webbackground/ship/shiporg";
		}

	}

}
